package servlet;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Md5Utils {

    private static final String UTF8 = "utf-8";

    /**
     * 加密
     *
     * @param password 待加密字符串
     * @return
     */
    static String encoder(String password)
            throws NoSuchAlgorithmException, UnsupportedEncodingException {
        byte[] bytes = password.getBytes(UTF8);
        MessageDigest mdInst = MessageDigest.getInstance("MD5");
        mdInst.update(bytes);
        byte[] md = mdInst.digest();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < md.length; i++) {
            int val = ((int) md[i]) & 0xff;
            if (val < 16) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(val));
        }
        return sb.toString();
    }

}